#!/usr/bin/env python
#coding=utf-8

import wx
from panel import Panel

class LearnPanel(Panel):
    ''' Representation of the lear panel. '''

    def __init__(self, parent, config):
        super(LearnPanel, self).__init__(parent, -1, wx.DefaultPosition,
                                    wx.DefaultSize, wx.NB_TOP)

        self.config = config
        self.create_sizer()
        self.add_direction_choice(
            config.get('Settings','lang1'),config.get('Settings','lang2'))
        self.add_set_choice()
        self.add_category_text()
        self.add_effectless_label()
        self.add_category_choice_lst()
        self.add_cb_effectless()
        self.add_oldest_first()
        self.add_cb_oldestFirst()
        self.add_start_button()

        self.create_learn_frame()

        self.frameSizer.Add(self.sizer)
        self.frameSizer.Add(self.bottomPanel,flag=wx.TOP,border=50)

        self.SetSizer(self.frameSizer)

    def create_learn_frame(self):
        ''' direction = "lang1" or "lang2". '''

        self.bottomPanel = wx.Panel(self,-1)
        self.bottomPanel.SetSizer(self.learnSizer)

        self.set_question_label('XXXXXXX')
        self.add_question_input()
        self.add_loudspeaker()

        self.set_answer_label('YYYYYYY')
        self.add_answer_input()
        self.add_state_input()
        self.add_verify_button()
        self.set_category_label()
        self.add_category_lst()

        self.hide_bottom_panel()

    def add_cb_effectless(self):
        '''Add the checkbox for learning with no effect.

        What does this mean? Well, if you toggle this checkbox
        you can surpress updating the error table.
         '''

        self.cb_effectless = wx.CheckBox(self, -1,)
        self.innerSizer3.Add(self.cb_effectless, flag=wx.TOP)

    def add_cb_oldestFirst(self):
        '''Add the checkbox for learning the oldest first.
         '''

        self.cb_oldestFirst = wx.CheckBox(self, -1,)
        self.innerSizer3.Add(self.cb_oldestFirst, flag=wx.TOP)
        self.innerSizer2.Add(self.innerSizer3,flag=wx.TOP,border=6)

    def add_effectless_label(self):
        ''' Sets the label "Dry Run". '''

        self.effectLabel = wx.StaticText(self, -1,
                                        label='Dry Run'
                                        )

        self.innerSizer.Add(self.effectLabel,flag=wx.TOP,border=15)

    def add_oldest_first(self):
        '''Sets the label "OldestFirst".'''

        self.oldestLabel = wx.StaticText(self, -1,
                                         label="   Oldest First    ")
        self.innerSizer3.Add(self.oldestLabel, flag=wx.TOP, border=2)


    def set_category_label(self):
        ''' Sets the label "Category". '''

        self.catLabel = wx.StaticText(self.bottomPanel, label='Categories')
        self.learnSizer.Add(self.catLabel, (0,6))

    def add_category_lst(self):
        '''Shows category list. '''

        self.voc_categories = wx.ListBox(self.bottomPanel, -1,
            size = wx.Size(150,150), choices = [])
        self.voc_categories.SetBackgroundColour('lightgray')
        self.learnSizer.Add(self.voc_categories, (1,6), span=(10,1))

    def update_voc_categories(self, lst):
        ''' Updates self.voc_categories with a given lst. '''
        self.voc_categories.Set(lst)

    def add_direction_choice(self, lang1, lang2):
        ''' lang1->lang2 or lang2->lang1.'''

        maxLen =max(len(lang1),len(lang2))
        n1 = maxLen - len(lang1)
        n2 = maxLen - len(lang2)
        direction1 = lang1 + n1*' '+'-> '+ lang2
        direction2 = lang2 + n2*' '+'  -> '+ lang1
        directionLst = [direction1,direction2]

        self.directionBox = wx.RadioBox(
            self, -1, label='Direction', pos=wx.DefaultPosition,
            size=wx.DefaultSize, choices=directionLst, style=wx.RA_VERTICAL)

        self.sizer.Add(
            self.directionBox,(0,1), (2,2), flag=wx.EXPAND | wx.TOP, border=20)

    def add_set_choice(self):
        ''' Learn all or erraneous vocs.'''

        setLst = ['All', 'Erraneous']
        self.setBox = wx.RadioBox(
            self, -1, label='Set', pos=wx.DefaultPosition,
            size=wx.DefaultSize, choices=setLst, style=wx.RA_VERTICAL)

        self.sizer.Add(
            self.setBox,(0,3),(2,2), flag=wx.EXPAND | wx.TOP, border=20)

    def add_category_text(self):
        ''' Add a textfield with the label "Category". '''

        self.catCtrl = wx.StaticText(self,-1,label='Category', style=wx.BORDER_SUNKEN)
#        self.sizer.Add(self.catCtrl, (0,7),(2,1),flag=wx.EXPAND|wx.TOP, border=30)
        self.innerSizer.Add(self.catCtrl)
        self.sizer.Add(self.innerSizer,(0,5), flag=wx.EXPAND|wx.TOP, border=37)

    def add_category_choice_lst(self):
        ''' Add an empty choice lst.

            Choice list will be filled by the presentation logic.
         '''

        self.categoryChoice = wx.Choice(
            self, -1, pos=wx.DefaultPosition, size = (200,25))
        #self.sizer.Add(
        #    self.categoryChoice, (0,8), flag=wx.TOP, border=36)
        self.innerSizer2.Add(self.categoryChoice)
        self.sizer.Add(self.innerSizer2,(0,6), flag=wx.EXPAND|wx.TOP, border=37)

    def add_start_button(self):
        ''' Start button.

            This button is deactivated during a learning session.
         '''

        self.startBtn = wx.Button(self, label='Start')
        self.sizer.Add(self.startBtn, (3,1),(1,7),flag=wx.EXPAND,border=5)

    def get_answer_input(self):
        ''' Returns the string from self.answerInput. '''

        return self.answerInput.GetValue()

    def get_category(self):
        ''' Returns the selected category name. '''

        idx = self.categoryChoice.GetSelection()
        return self.categoryChoice.GetString(idx)

    def get_cb_effectless(self):
        '''Returns True|False for none-effective.'''

        return self.cb_effectless.IsChecked()

    def get_cb_oldestFirst(self):
        '''Returns True|False for learn oldestFirst.'''

        return self.cb_oldestFirst.IsChecked()

    def get_direction_box(self):
        ''' '''
        idx = self.directionBox.GetSelection()
        return self.directionBox.GetString(idx)

    def get_question(self):
        ''' Returns the string from self.questionInput. '''

        return self.questionInput.GetValue()

    def get_set_box(self):
        ''' Returns string value of set box: All, Erraneous. '''

        idx = self.setBox.GetSelection()
        return self.setBox.GetString(idx)

    def get_start_btn_label(self):
        return self.startBtn.GetLabel()

    def create_sizer(self):
        ''' Place our sizer on the panel.

            This is my very first panel created with a GridBagSizer.
         '''

        self.frameSizer = wx.BoxSizer(orient=wx.VERTICAL)
        self.sizer = wx.GridBagSizer(10,20)
        self.learnSizer = wx.GridBagSizer(10,10)
        self.innerSizer = wx.BoxSizer(orient=wx.VERTICAL)
        self.innerSizer2 = wx.BoxSizer(orient=wx.VERTICAL)
        self.innerSizer3 = wx.BoxSizer(orient=wx.HORIZONTAL)
        #TODO: replace innerSizer and innerSizer2 with one gridBagSizer



    def enable_cb_effectless(self):
        '''Show the effectless checkbox after learning. '''

        self.cb_effectless.Enable()
        self.effectLabel.Enable()

    def enable_cb_oldest(self):
        '''Show the oldestFirst label and checkbox.'''
        self.cb_oldestFirst.Enable()
        self.oldestLabel.Enable()


    def enable_directionBox(self):
        ''' Show the directionBox after learning.  '''

        self.directionBox.Enable()

    def enable_setBox(self):
        ''' Show the setBox after learning.  '''

        self.setBox.Enable()

    def enable_catCtrl(self):
        ''' Show the text Category after learning. '''
        self.catCtrl.Enable()

    def enable_categoryChoice(self):
        ''' Show the choice list after learning. '''

        self.categoryChoice.Enable()

    def hide_directionBox(self):
        ''' Hide the directionBox during learning.  '''
        self.directionBox.Disable()

    def hide_setBox(self):
        ''' Hide the setBox during learning.  '''

        self.setBox.Disable()

    def hide_catCtrl(self):
        ''' Hide the text Category during learning. '''
        self.catCtrl.Disable()

    def hide_categoryChoice(self):
        ''' Hide the choice list during learning. '''

        self.categoryChoice.Disable()

    def hide_cb_effectless(self):
        '''Hide the effectless checkbox during learning. '''

        self.cb_effectless.Disable()
        self.effectLabel.Disable()

    def hide_cb_oldest(self):
        '''Hide the oldest label and checkbox.'''

        self.cb_oldestFirst.Disable()
        self.oldestLabel.Disable()


    def hide_bottom_panel(self):
        self.bottomPanel.Hide()
        self.Layout()

    def update_answer_label(self, label):
        ''' Update a dummy label for the Labels "Lang1" or "Lang2". '''

        self.answerLabel.SetLabel(label)

    def update_answer_input(self, text):
        ''' Update the field answerInput with text. '''

        self.answerInput.SetValue(text)

    def update_question_label(self, label):
        ''' Update a dummy label for the Labels "Lang1" or "Lang2". '''

        self.questionLabel.SetLabel(label)

    def update_question_input(self, voc):
        ''' Update the question. '''

        self.questionInput.SetValue(voc)

    def update_question_input_tooltip(self, msg):
        tip = wx.ToolTip(msg)
        self.questionInput.SetToolTip(tip)

    def update_state_input(self, value):
        '''Diplays err_lang1 or value from error table.'''

        self.stateInput.SetLabel(value)

    def show_bottom_panel(self):
        self.bottomPanel.Show()
        self.Layout()

    def set_choicelst(self, lst):
        ''' Fill our categoryChoice list with values. '''

        self.categoryChoice.SetItems(lst)

    def set_focus_on_answer(self):
        ''' Set focus. '''

        self.answerInput.SetFocus()

    def set_start_btn_label(self, str):
        ''' Set the button label at str. '''

        self.startBtn.SetLabel(str)

    def set_state_colour(self, colour):
        ''' The colours shall be black or red '''

        self.stateInput.SetForegroundColour(colour)

    def set_question_label(self, label):
        ''' Sets a dummy label for the Labels "Lang1" or "Lang2". '''

        self.questionLabel = wx.StaticText(self.bottomPanel, label=str(label))
        self.learnSizer.Add(self.questionLabel, (0,1))

    def add_loudspeaker(self):
        self.loudspeaker = wx.BitmapButton(self.bottomPanel, -1,
                                    wx.Bitmap('gui/icons/speaker.png'))
        self.learnSizer.Add(self.loudspeaker, (0,4))

    def add_question_input(self):
        ''' Add an input field for the question. '''

        self.questionInput = wx.TextCtrl(
            self.bottomPanel,-1,value='placeholder', size=(300,25))
        self.learnSizer.Add(
            self.questionInput, (0,3))

    def set_answer_label(self, label):
        ''' Sets a dummy label for the Labels "Lang1" or "Lang2". '''

        self.answerLabel = wx.StaticText(self.bottomPanel, label=str(label))
        self.learnSizer.Add(self.answerLabel, (1,1))

    def add_answer_input(self):
        ''' Add an input field for the question. '''

        self.answerInput = wx.TextCtrl(
                    self.bottomPanel,-1,value='', size=(300,25),
                    style = wx.TE_PROCESS_ENTER)
        self.learnSizer.Add(self.answerInput, (1,3))

    def add_state_input(self):
        ''' Add an input field for the question. '''
        #self.stateInput = wx.TextCtrl(
        #    self.bottomPanel,-1,value='42',size=(30,25))
        self.stateInput = wx.StaticText(
                    self.bottomPanel,-1,label='42',size=(30,25))
        self.learnSizer.Add(self.stateInput, (0,5))

    def add_verify_button(self):
        ''' Verify button.

            Verify the user input after pressing this button
         '''

        self.verifyBtn = wx.Button(self.bottomPanel, label='Verify')
        self.learnSizer.Add(self.verifyBtn, (3,3),flag=wx.ALIGN_CENTER)
